var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all) __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if ((from && typeof from === 'object') || typeof from === 'function') {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, {
          get: () => from[key],
          enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
        });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (
  (target = mod != null ? __create(__getProtoOf(mod)) : {}),
  __copyProps(
    // If the importer is in node compatibility mode or this is not an ESM
    // file that has been converted to a CommonJS file using a Babel-
    // compatible transform (i.e. "__esModule" has not been set), then set
    // "default" to the CommonJS "module.exports" for node compatibility.
    isNodeMode || !mod || !mod.__esModule
      ? __defProp(target, 'default', { value: mod, enumerable: true })
      : target,
    mod,
  )
);
var __toCommonJS = (mod) => __copyProps(__defProp({}, '__esModule', { value: true }), mod);

// src/Cascader/LazyCascader/index.tsx
var LazyCascader_exports = {};
__export(LazyCascader_exports, {
  default: () => LazyCascader_default,
});
module.exports = __toCommonJS(LazyCascader_exports);
var import_react = __toESM(require('react'));
var import_antd = require('antd');
var import_createField = __toESM(require('../../createField'));
var import_FieldContext = require('../../_utils/contexts/FieldContext');
var import_useRequestOptions = __toESM(require('../../_utils/hooks/useRequestOptions'));
var LazyCascader = ({
  children,
  fieldNames,
  value,
  onChange,
  options: optionRequest,
  ...props
}) => {
  const { label } = (0, import_FieldContext.useFieldContext)();
  const { value: valueKey = 'value', children: childrenKey = 'children' } = fieldNames || {};
  const { options, setOptions } = (0, import_useRequestOptions.default)(optionRequest);
  const loadData = (0, import_react.useCallback)(
    (selectedOptions) => {
      const targetOption = selectedOptions[selectedOptions.length - 1];
      targetOption.loading = true;
      optionRequest(targetOption).then((opts) => {
        targetOption.loading = false;
        targetOption[childrenKey] = opts;
        setOptions([...options]);
      });
    },
    [optionRequest, options],
  );
  (0, import_react.useEffect)(() => {
    if (
      (value == null ? void 0 : value.length) &&
      (value == null ? void 0 : value.length) > 1 &&
      (options == null ? void 0 : options.length)
    ) {
      let currentOptions = [...options];
      let currentOption = options.find((option) => {
        return option[valueKey] === value[0][valueKey];
      });
      if (currentOption && !currentOption[childrenKey]) {
        currentOption.loading = true;
        Promise.all(
          value.slice(0, value.length - 1).map((parent) => {
            return optionRequest(parent);
          }),
        ).then((allOpts) => {
          allOpts.forEach((opts, idx) => {
            if (!currentOption) return;
            currentOption.loading = false;
            currentOption[childrenKey] = opts;
            currentOption = currentOption[childrenKey].find((childOption) => {
              return childOption[valueKey] === value[idx + 1][valueKey];
            });
          });
          setOptions(currentOptions);
        });
      }
    }
  }, [options, value]);
  return (
    // @ts-ignore
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Cascader,
      {
        placeholder: `请选择${label || ''}`,
        ...props,
        fieldNames,
        options,
        loadData,
        value: value ? value.map((item) => item[valueKey]) : value,
        onChange: (_, selectedOptions) => {
          onChange == null ? void 0 : onChange(selectedOptions);
        },
        changeOnSelect: true,
      },
      children,
    )
  );
};
var LazyCascader_default = (0, import_createField.default)(LazyCascader, {
  defaultDetailType: 'cascader',
});
